<?php

namespace App\Admin\Controllers;

use App\Services\HardwareListService;
use Slowlyo\OwlAdmin\Controllers\AdminController;

/**
 * 硬件管理
 *
 * @property HardwareListService $service
 */
class HardwareListController extends AdminController
{
	protected string $serviceName = HardwareListService::class;

	public function list()
	{
		$crud = $this->baseCRUD()
			->filter($this->baseFilter()->body([

				amis()->SelectControl('category', '硬件分类')->size('md')
                    ->source('/hardware_category/?_action=getData')
					->labelField('category_name')
					->valueField('id')
                    ->clearable(),
					amis()->SelectControl('statuss', '状态')->size('md')
					->options(
						['1' => '使用中', '2' => '未使用', '3' => '报废']
					),
                
                amis()->TextControl('brand_name', '品牌型号')->size('md')->clearable(),
				
				amis()->TextControl('configuration', '硬件配置')->size('md')->clearable(),

				amis()->TextControl('sn_code', '硬件SN码')->size('md')->clearable(),
				
            ]))
			->headerToolbar([
				$this->createButton('dialog'),
				...$this->baseHeaderToolBar()
			])
			->columns([
				
				amis()->TableColumn('thecategory.category_name', '硬件分类'),
				amis()->TableColumn('brand_name', '品牌型号'),
				amis()->TableColumn('sn_code', '硬件SN码'),
				amis()->TableColumn('configuration', '硬件配置'),
				amis()->TableColumn('purchase_date', '购买日期')->type('date'),
				amis()->TableColumn('location.location_name', '当前位置')->sortable(),
				amis()->TableColumn('user_department', '使用方'),
				amis()->TableColumn('statuss', '状态')->type('mapping')->map([
					1 => "<span class='label label-success'>使用中</span>",
					2 => "<span class='label label-warning'>未使用</span>",
					3 => "<span class='label label-danger'>报废</span>",
				]),



				amis()->TableColumn(' ', '操作')->type('operation')->buttons([
					$this->showmove('dialog'),
                ]),
				amis()->TableColumn('updated_at', '最后更新时间')->type('date')->sortable(),

				$this->rowActions('dialog')
			]);

		return $this->baseList($crud);
	}

	public function form($isEdit = false)
	{
		return $this->baseForm()->body([

			amis()->SelectControl('category', '硬件分类')->searchable(true)
				->source('/hardware_category/?_action=getData')
				->labelField('category_name')
				->valueField('id')
				->autoFill([
					'xmodel' => '${xmodel}'
				]),
			amis()->TextControl('brand_name', '品牌型号'),

			amis()->TextControl('sn_code', '硬件SN码'),

			amis()->TextareaControl('configuration', '硬件配置')->value('${configuration?configuration:xmodel}'),

			amis()->TimeControl('purchase_date', '购买日期')->type('input-date'),

			// 改成调用分类表
			amis()->SelectControl('current_location', '当前位置')->searchable(true)
			->source('/location_category/?_action=getData')
			->labelField('location_name')
			->valueField('id'),
			amis()->TextControl('user_department', '使用方'),

			amis()->SelectControl('statuss', '状态')->options(
				['1' => '使用中', '2' => '未使用', '3' => '报废']
			),
			
		]);
	}

	public function detail()
	{
		return $this->baseDetail()->body([
			amis()->TextControl('id', 'ID')->static(),
			amis()->TextControl('add_time', '添加时间')->static(),
			amis()->TextControl('brand_name', '硬件品牌')->static(),
			amis()->TextControl('configuration', '硬件硬件配置')->static(),
			amis()->TextControl('current_location', '当前位置')->static(),
			amis()->TextControl('editor_uid', '编辑人UID')->static(),
			amis()->TextControl('hardware_name', '硬件名称')->static(),
			amis()->TextControl('model_number', '硬件型号')->static(),
			amis()->TextControl('operator_uid', '操作人UID')->static(),
			amis()->TextControl('purchase_date', '购买日期')->static(),
			amis()->TextControl('sn_code', '硬件SN码')->static(),
			amis()->TextControl('soft_delete', '软删除标识（0未删除，1已删除）')->static(),
			amis()->TextControl('update_time', '更新时间')->static(),
			amis()->TextControl('user_department', '使用方')->static(),
			amis()->TextControl('created_at', admin_trans('admin.created_at'))->static(),
			amis()->TextControl('updated_at', admin_trans('admin.updated_at'))->static(),
		]);
	}

	public function showmove()
	{
		return amis()->DrawerAction()
			->label('变更记录')
			->icon('fa-solid fa-chart-line')
			->level('info')
			->drawer(
				amis()->Drawer()
					->title('硬件变更记录 - ${brand_name}(${sn_code})')
					->size('lg')
					->body([
						// 添加变更记录按钮
						amis()->DialogAction()
							->label('添加变更记录')
							->icon('fa fa-plus')
							->level('primary')
							->dialog(
								amis()->Dialog()
									->title('添加变更记录')
									->size('md')
									->body(
										amis()->Form()
											->api('POST:/hardware_move_record')
											->body([
												amis()->HiddenControl('hardware_id')->value('${id}'),
												amis()->HiddenControl('sn_code')->value('${sn_code}'),
												amis()->HiddenControl('previous_location')->value('${current_location}'),
												
												amis()->SelectControl('after_location', '变更后位置')->required()
													->source('/location_category/?_action=getData')
													->labelField('location_name')
													->valueField('id'),
												
												amis()->DateTimeControl('move_time', '变更时间')->type('input-date')->required(),
												
												amis()->TextareaControl('move_remark', '变更备注')->rows(3),
											])
											// 添加成功后更新硬件表的当前位置
											->onEvent([
												'submitSucc' => [
													'actions' => [
														[
															'actionType' => 'reload',
															'target' => 'move-record-table'
														]
													]
												]
											])
									)
									->actions([
										amis()->Button()->label('取消')->actionType('cancel'),
										amis()->Button()->label('确认添加')->level('primary')->type('submit'),
									])
							),

						amis()->Divider(),

						// 显示历史记录
						amis()->CRUDTable()->style([
							'border' => 'none',
							'padding' => '0px',
						])
							->api('GET:/hardware_move_record?_action=getData&hardware_id=${id}')
							->syncLocation(false)
							->perPage(10)
							->columns([
								amis()->TableColumn('move_time', '变更时间')->type('date'),
								amis()->TableColumn('prelocation.location_name', '变更前位置'),
								amis()->TableColumn('afterlocation.location_name', '变更后位置'),
								amis()->TableColumn('move_remark', '变更备注')->width(200),
								amis()->TableColumn('created_at', '记录时间')->type('datetime'),
							])
							->headerToolbar([])
							->footerToolbar(['pagination'])
					])
			);
	}
						



}
